#include "SetWidget.h"

#include <QLabel>
#include <QLineEdit>
#include <QDateTimeEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QDebug>

SetWidget::SetWidget(QWidget *parent) : QWidget(parent)
{
    Init();
}

void SetWidget::SlotConfirmButtonClicked()
{
    close();

    auto xRangeMin = QDateTime::fromString(xRangeMinEdit->text(), "yyyy-MM-dd hh:mm:ss").toSecsSinceEpoch();
    auto xRangeMax = QDateTime::fromString(xRangeMaxEdit->text(), "yyyy-MM-dd hh:mm:ss").toSecsSinceEpoch();
    auto yRangeMin = yRangeMinEdit->text().toDouble();
    auto yRangeMax = yRangeMaxEdit->text().toDouble();

    if (xRangeMin >= xRangeMax)
    {
        return;
    }
    if (yRangeMin >= yRangeMax)
    {
        return;
    }

    emit SignalConfirmButtonClicked(xRangeMin, xRangeMax, yRangeMin, yRangeMax);
}

void SetWidget::SlotCancelButtonClicked()
{
    close();
}

void SetWidget::Init()
{
    setFixedSize(385, 155);

    auto mainLayout = new QVBoxLayout;
    mainLayout->setMargin(20);

    auto rangeGridLayout = new QGridLayout;
    rangeGridLayout->setVerticalSpacing(10);

    auto xRangeLabel = new QLabel("xRange:", this);
    auto xRangeHLayout = new QHBoxLayout;
    xRangeHLayout->setSpacing(3);
    xRangeMinEdit = new QDateTimeEdit(QDateTime::currentDateTime(), this);
    xRangeMinEdit->setDisplayFormat("yyyy-MM-dd hh:mm:ss");
    xRangeMinEdit->setCalendarPopup(true);
    xRangeMinEdit->setFixedSize(142, 30);
    auto xLineLabel = new QLabel("-", this);
    xRangeMaxEdit = new QDateTimeEdit(QDateTime::currentDateTime(), this);
    xRangeMaxEdit->setDisplayFormat("yyyy-MM-dd hh:mm:ss");
    xRangeMaxEdit->setCalendarPopup(true);
    xRangeMaxEdit->setFixedSize(142, 30);
    xRangeHLayout->addStretch();
    xRangeHLayout->addWidget(xRangeMinEdit);
    xRangeHLayout->addWidget(xLineLabel);
    xRangeHLayout->addWidget(xRangeMaxEdit);
    rangeGridLayout->addWidget(xRangeLabel, 0, 0);
    rangeGridLayout->addLayout(xRangeHLayout, 0, 1);

    auto yRangeLabel = new QLabel("yRange:", this);
    auto yRangeHLayout = new QHBoxLayout;
    yRangeHLayout->setSpacing(3);
    yRangeMinEdit = new QLineEdit(this);
    yRangeMinEdit->setFixedSize(142, 30);
    auto yLineLabel = new QLabel("-", this);
    yRangeMaxEdit = new QLineEdit(this);
    yRangeMaxEdit->setFixedSize(142, 30);
    yRangeHLayout->addStretch();
    yRangeHLayout->addWidget(yRangeMinEdit);
    yRangeHLayout->addWidget(yLineLabel);
    yRangeHLayout->addWidget(yRangeMaxEdit);
    rangeGridLayout->addWidget(yRangeLabel, 1, 0);
    rangeGridLayout->addLayout(yRangeHLayout, 1, 1);

    auto bottomLayout = new QHBoxLayout;
    auto confirmButton = new QPushButton("确认", this);
    confirmButton->setFixedSize(100, 28);
    auto cancelButton = new QPushButton("取消", this);
    cancelButton->setFixedSize(100, 28);
    bottomLayout->addStretch();
    bottomLayout->addWidget(confirmButton);
    bottomLayout->addWidget(cancelButton);
    mainLayout->addLayout(rangeGridLayout);
    mainLayout->addLayout(bottomLayout);
    setLayout(mainLayout);

    connect(confirmButton, &QPushButton::clicked, this, &SetWidget::SlotConfirmButtonClicked);
    connect(cancelButton, &QPushButton::clicked, this, &SetWidget::SlotCancelButtonClicked);
}
